package com.sk.sk_frame_cloud.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.sk_frame_cloud.constants.CommonConstant;
import com.sk.sk_frame_cloud.constants.ProManageStatus;
import com.sk.sk_frame_cloud.entity.SysProManage;
import com.sk.sk_frame_cloud.mapper.SysProManageMapper;
import com.sk.sk_frame_cloud.param.ProManageParam;
import com.sk.sk_frame_cloud.res.CommonResponse;
import com.sk.sk_frame_cloud.service.ISysProManageService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2023-12-15
 */
@Service
public class SysProManageServiceImpl extends ServiceImpl<SysProManageMapper, SysProManage> implements ISysProManageService {

    @Override
    public CommonResponse proNameIsExist(ProManageParam proManageParam) {

        HashMap<String,String> checkResult = new HashMap<>();

        QueryWrapper<SysProManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("pro_name","pro_code");
        List<SysProManage> manageList = this.list(queryWrapper);
        List<String> proNameList = manageList.stream().map(SysProManage::getProName).collect(Collectors.toList());
        List<String> proCodeList = manageList.stream().map(SysProManage::getProCode).collect(Collectors.toList());

        //检测名称是否重复
        if (proCodeList.contains(proManageParam.getProCode())){
            checkResult.put("proName","项目名已存在");
        }
        if (proNameList.contains(proManageParam.getProName())){
            checkResult.put("proCode","项目代码已存在");
        }

        if (checkResult.isEmpty()) {
            return CommonResponse.ok();
        }else {
            return CommonResponse.error(checkResult);
        }

    }


    @Override
    public CommonResponse createPro(ProManageParam proManageParam) {

        HashMap<String, String> res = new HashMap<>();

        //创建之前先校验项目信息是否可用
        CommonResponse commonResponse = this.proNameIsExist(proManageParam);
        if (!commonResponse.getCode().equals(CommonConstant.SC_OK_200)) {
            return commonResponse;
        }
        //项目信息可用
        SysProManage sysProManage = new SysProManage();
        sysProManage.setSid(IdUtil.getSnowflakeNextIdStr());
        sysProManage.setProCode(proManageParam.getProCode());
        sysProManage.setProName(proManageParam.getProName());
        sysProManage.setProStatus(ProManageStatus.HAS_CREATED.getValue());
        sysProManage.setCreateTime(LocalDateTime.now());
        sysProManage.setUpdateTime(LocalDateTime.now());

        //持久化
        this.save(sysProManage);

        res.put("sid",sysProManage.getSid());

        return CommonResponse.ok("项目创建成功",res);


    }

    @Override
    public void updateProStatus(String proStatus, String proManageSid) {

        this.update(new UpdateWrapper<SysProManage>().eq("sid",proManageSid).set("pro_status",proStatus));

    }

    @Override
    public String getProCode(String proSid) {

        SysProManage sysProManage = this.getById(proSid);
        if (Objects.nonNull(sysProManage)){
            return sysProManage.getProCode();
        }
        return null;
    }
}
